#include "_old_ErrorHandler.h"

namespace mp_error
{
	ErrorHandler::ErrorHandler()
	{
		m_errorListeners = NULL;
		m_errorListenerCount = 0;
	}

	ErrorHandler::~ErrorHandler()
	{
		if ( m_errorListeners != NULL )
		{
			for(int i = 0; i < m_errorListenerCount; i++)
			{
				delete m_errorListeners[i];
			}
			delete [] m_errorListeners;
		}
	}


	void ErrorHandler::WriteError(const char* msg)
	{
		//If we are writing to the console
		if ((m_errorWriteFlags & 
				static_cast<ErrorFlags>(ERW_CONSOLE)) == 
			static_cast<ErrorFlags>(ERW_CONSOLE))
		{
			WriteErrorToConsole(msg);
		}

		//If we are writing to a file
		if ((m_errorWriteFlags & 
				static_cast<ErrorFlags>(ERW_FILE)) == 
			static_cast<ErrorFlags>(ERW_FILE))
		{
			WriteErrorToFile(msg);
		}

		//If we are sending events
		if ((m_errorWriteFlags & 
				static_cast<ErrorFlags>(ERW_EVENT)) == 
			static_cast<ErrorFlags>(ERW_EVENT))
		{
			SendErrorEvents(msg);
		}
	}

	void ErrorHandler::WriteError(const string msg)
	{
		//If we are writing to the console
		if ((m_errorWriteFlags & 
				static_cast<ErrorFlags>(ERW_CONSOLE)) == 
			static_cast<ErrorFlags>(ERW_CONSOLE))
		{
			WriteErrorToConsole(msg.c_str());
		}

		//If we are writing to a file
		if ((m_errorWriteFlags & 
				static_cast<ErrorFlags>(ERW_FILE)) == 
			static_cast<ErrorFlags>(ERW_FILE))
		{
			WriteErrorToFile(msg);
		}

		//If we are sending events
		if ((m_errorWriteFlags & 
				static_cast<ErrorFlags>(ERW_EVENT)) == 
			static_cast<ErrorFlags>(ERW_EVENT))
		{
			SendErrorEvents(msg);
		}
	}

	void ErrorHandler::WriteErrorToFile(const string msg)
	{
		//Not Available
	}

	void ErrorHandler::SendErrorEvents(const string msg)
	{
		for(int i = 0; i < m_errorListenerCount; i++)
		{
			//If we fail to add an error message
			if ( !m_errorListeners[i]->AddErrorMessage(msg) )
			{
				//Print the error set by the error event
				WriteErrorToConsole(
					m_errorListeners[i]->
					PeekLastErrorMessage().c_str()
				);
			}
		}
	}


	void ErrorHandler::WriteErrorToConsole(const char* msg)
	{
		printf("MPGL Error: ");
		printf(msg);
		printf("\n");
	}

	void ErrorHandler::AddErrorEventListener(ErrorEvent* listener)
	{
		if ( m_errorListenerCount == 0 )
		{
			m_errorListeners = new ErrorEvent*[1];
			m_errorListeners[m_errorListenerCount++] = listener;
		}
		else
		{
			throw "Needs to be implemented!";
		}
	}


}